<?php

/*
 * Copyright 2017 zhc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Description of DTableEditor
 * Display the table editor
 * @author Zhu Chen
 * @package TableEditor
 */
class DTableEditor implements IPackage{
    
    /**
     * The instance of table editor
     * @var TableEditor 
     */
    protected $teditor;

    protected function __construct(TableEditor $tedit) {
        $this->teditor = $tedit;
    }
    
    /**
     * Implement the function in the IPackage
     * @param type $action
     * @return string
     */
    public static function get_function($action): string {
        return 'show_page';
    }
    
    /**
     * Implement the function in the table editor
     * @param string $action
     * @return type array
     */
    public static function get_includes($action) {
        return ['tables/tableeditor'];
    }
    
    /**
     * Display the page of table editor
     */
    public function show_page(){
        $smarty = core::tpl();
        $smarty->assign('title',$this->teditor->title);
        $smarty->assign('bnadd',$this->teditor->btnadd_display);
        $smarty->assign('bndel',$this->teditor->btndel_display);
        $smarty->assign('bnmod',$this->teditor->btnmod_display);
        $smarty->assign('bnsearch',$this->teditor->btnsearch_display);
        $cols=[];$i=0;
        foreach($this->teditor->getColumns() as $col)   {
            $cols[$i]['fname'] = $col->name;
            $cols[$i]['disp'] = $col->display;
            $cols[$i]['readonly'] = $col->readonly;
            $cols[$i]['description'] = $col->description;
            $cols[$i]['type'] = $col->type;
            $i++;
        }
        $smarty->assign('cols', $cols);
        $smarty->display('tables/index.tpl',$this->teditor->title);
    }
}
